package com.zking.dao;

import com.zking.entity.HistoryItem;
import com.zking.util.DBUtil;

import java.util.List;

public class HistoryDao {

    public static List<HistoryItem> getHistoryItemList(String user_id, int maxCount) {
        String sql = "select * from easybuy_history where eu_user_id = ?";
        List<HistoryItem> list = DBUtil.query(sql, HistoryItem.class, user_id);
        list.sort((o1, o2) -> o2.getId() - o1.getId());

        // 清空大于2条之前的记录
        for(int i=maxCount; i<list.size(); i++){
            deleteHistoryItem(list.get(i).getId());
        }

        return list.subList(0, Math.min(maxCount, list.size()));
    }

    public static boolean addHistoryItem(HistoryItem item) {
        String sql = "insert into easybuy_history(eu_user_id,ep_id,ep_name,ep_image) values(?,?,?,?)";
        return DBUtil.update(sql, item.getEu_user_id(), item.getEp_id(), item.getEp_name(), item.getEp_image());
    }

    public static boolean deleteHistoryItem(int id) {
        String sql = "delete from easybuy_history where id = ?";
        return DBUtil.update(sql, id);
    }
}
